package com.rmt.www.daoImpl;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.rmt.www.dao.UserDao;
import com.rmt.www.vo.User;


public class UserDaoImpl implements UserDao{

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	

	public void insert(User user) {
		jdbcTemplate.update("insert into user(type,name,password) values(?,?,?)", new Object[]{user.getType(),user.getName(),user.getPassword()}, 
				new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});
	}
	
	public User getUser(int uid) {
		try {	
			String sql = "select * from user where uid = ?";
			
		    final Object[] params = new Object[] {uid};	
		    
			return  (User) this.jdbcTemplate.queryForObject(sql,params, new UserRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
		
	}


	public User getUser(String name) {
		try {	
			String sql = "select * from user where name = ?";
			
		    final Object[] params = new Object[] {name};	
		    
			return  (User) this.jdbcTemplate.queryForObject(sql,params, new UserRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
		
	}

}
